package com.ita.acmsystem.merchant.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.acmsystem.merchant.entity.FoodTypes;
import com.ita.acmsystem.merchant.entity.Foods;
import com.ita.acmsystem.merchant.entity.Merchant;
import com.ita.acmsystem.merchant.service.IFoodService;
import com.ita.acmsystem.merchant.service.IFoodTypesService;
import com.ita.acmsystem.merchant.service.IMerchantService;
import com.ita.acmsystem.merchant.service.impl.FoodServiceImpl;
import com.ita.acmsystem.merchant.service.impl.FoodTypesServiceImpl;
import com.ita.acmsystem.merchant.service.impl.MerchantServiceImpl;

/**
 * Servlet implementation class ContentServlet
 */
public class ContentServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private IFoodService foodService = new FoodServiceImpl();
	private IFoodTypesService foodTypesService = new FoodTypesServiceImpl();
	private IMerchantService merchantService = new MerchantServiceImpl();

	private Map<String, String> foodTypesMap = null;
	private Map<String, String> merchantsMap = null;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ContentServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		getContent(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		getContent(request, response);
	}

	private void getContent(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		Merchant user = (Merchant) session.getAttribute("user");

		String merchantName = user.getMerchantName();
		String merchantPic = user.getMerchantPic();

		// List<Foods> foodsList = foodService.getAllFoods();
		List<Foods> foodsList = foodService.getFoodsByMerchantId(user
				.getMerchantId());

		if (foodTypesMap == null) {
			List<FoodTypes> foodTypesList = foodTypesService.getAllFoodTypes();
			foodTypesMap = new HashMap<String, String>();
			for (FoodTypes foodTypes : foodTypesList) {
				foodTypesMap
						.put(foodTypes.getTypeId(), foodTypes.getTypeName());
			}
		}

		// List<Merchant> merchantsList = merchantService.getAllMerchant();
		// merchantsMap = new HashMap<String, String>();
		// for (Merchant merchant : merchantsList) {
		// merchantsMap.put(merchant.getMerchantId(),
		// merchant.getMerchantName());
		// }

		request.setAttribute("merchantName", merchantName);
		request.setAttribute("merchantPic", merchantPic);

		request.setAttribute("foodsList", foodsList);
		request.setAttribute("foodTypesMap", foodTypesMap);
		// request.setAttribute("merchantsMap", merchantsMap);

		// request.getRequestDispatcher("content.jsp").forward(request,
		// response);

		request.getRequestDispatcher("beatifulcontent.jsp").forward(request,
				response);
	}

}
